React = require('react')
{View,Image,Text,ScrollView,TouchableHighlight,StyleSheet,Navigator,ListView,TouchableOpacity,ActivityIndicator} = require('react-native')
Dimensions = require('Dimensions')
{ width, height } = Dimensions.get('window')
Drawer = require('antd-mobile/lib/drawer')
Platform = require('react-native')
DrawerLayout = require('react-native-drawer-layout')
moment = require('moment')
TopNotice = require('antd-mobile/lib/notice-bar')
_ = require('underscore')
{InteractionManager,View,Image,Text,ScrollView,TouchableHighlight,StyleSheet,Navigator,ListView,TouchableOpacity} = require('react-native')
RefreshControl = require('antd-mobile/lib/refresh-control')
# Drawer = require('antd-mobile/lib/drawer')
ActionSheet = require('antd-mobile/lib/action-sheet')
backImg = require('../img/back.png')
searchIcon = require('../img/search_white.png')

BasePage =
  getInitialState:->
    @ds = new ListView.DataSource rowHasChanged:(r1,r2)->r1 isnt r2
    # @gqState = 0
    {
      isFirstLoad:true
      ready:false
    }
  _processTime:(time)->
    t = moment(time)
    if t.isValid()
      t.format('DD/hhmm')
    else
      ""
  refreshList:->
    debugger
    @props.fetchRefresh(@state.selectedValue)
  onEndReached:(event)->
    return if @props.isLoading or @props.refreshing or @props.completed
    @props.fetchGet(@props.pageIndex+1,@state.selectedValue)
  componentDidMount:->
    # debugger
    @initFetchData2(@state.selectedValue)
  initFetchData2:(selectedValue)->
    @props.fetchRefresh(selectedValue) if @props.connected
    InteractionManager.runAfterInteractions =>
      @setState isFirstLoad:false,ready:true


  initFetchData:->
    debugger
    that = @
    if @props.connected
      setTimeout ->
        that.props.fetchRefresh()
        that.setState isFirstLoad:false
      ,600
    else
      @setState
        isFirstLoad:false
  delayLoad:(duration)->
    setTimeout =>
      debugger
      if @props.lastFetchTime
        lastTime = moment(@props.lastFetchTime)
        currTime = moment(new Date())
        if currTime.subtract(duration).isAfter(lastTime)
          @props.fetchRefresh(@gqState)
      else
        @props.fetchRefresh(@gqState)
    ,800
  _renderBody:->
    debugger
    if @state.isFirstLoad
      <View style={width:width,height:height,backgroundColor:'#ffffff'}/>
    else
      @_renderListView()
  _renderListHeader:->
    if @props.connected is false and @props.list.length>0
      <TopNotice mode="closable" style={{margin:10}}>
        无网络连接，您现在看到的是缓存数据
      </TopNotice>
  _renderListFooter:->
    if @props.completed
      <View style={{flexDirection:'row',justifyContent:'center',paddingTop:20,paddingBottom:30}}>
        <Text>到底了</Text>
      </View>
    else unless @props.refreshing
      <View style={{flexDirection:'row',justifyContent:'center',paddingTop:20,paddingBottom:30}}>
        <ActivityIndicator size='small'></ActivityIndicator>
        <Text style={{marginLeft:5}}>正在加载</Text>
      </View>
    else
      <View/>

  _renderListSeparator:(sectionID,rowID)->
    <View key={rowID} style={{height:1,backgroundColor:'#CCCCCC'}}/>
  _renderListView:->
    <ListView ref='listView' dataSource={@dataSource} style={{flex:1}}
          enableEmptySections={true}
          renderHeader={@_renderListHeader}
          renderRow={@_renderRow}
          renderSeparator={this._renderListSeparator}
          renderFooter={@_renderListFooter}
          initiaListSize:{20}
          pageSize={20}
          scrollEventThrottle={20}
          onEndReached={@onEndReached}
          onEndReachedThreshold={20}
          stickyHeader
          stickyProps={{
            stickyStyle: { zIndex: 999, top: 43 },
            topOffset: -43,
          }}
          refreshControl={
            <RefreshControl refreshing={@props.refreshing} onRefresh={@refreshList}
                distanceToRefresh={60}
                resistance={2.5}
                hammerOptions={{
                  touchAction:'auto'
                  recognizers:
                    pan:
                      threshold:10
                  }}/>
          }>
    </ListView>
  render:->
    if @state.filter
      debugger
      filterList = (v for v in @props.list when  v.VCPILOTNAME.indexOf(@state.filter) isnt -1)
      @dataSource = @ds.cloneWithRows(filterList)
    else
      @dataSource = @ds.cloneWithRows(@props.list)
    <View style={width:width,height:height,flexDirection:'column',backgroundColor:'#ffffff',paddingBottom:10}>
      {@_renderHeader()}
      {@_renderListColumns()}
      {@renderSearchBar?()}
      <View  style={{height:1,width:width,backgroundColor:'#CCCCCC'}}/>
      {
        if @state.ready then @_renderBody()
      }
    </View>
module.exports = BasePage
